package com.zhitan.ems.app.patrol.controller;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.patrol.entity.PatrolNavEntity;
import com.zhitan.ems.app.patrol.service.PatrolNavService;
import com.zhitan.ems.app.patrol.vo.PatrolNavVO;
import com.zhitan.ems.app.patrol.wrapper.PatrolNavWrapper;
import com.zhitan.ems.common.annotation.Log;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.enums.BusinessType;
import com.zhitan.ems.common.utils.DateUtils;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.common.utils.poi.ExcelUtil;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 导航
 * 
 * @author zhitan
 * 
 * @date 2024-12-04 20:14:59
 */
@Api(tags = "PC管理--巡检--导航", value = "导航")
@RestController
@RequestMapping("intra/patrol/patrolnav")
public class PatrolNavController extends BaseController {
	@Autowired
	private PatrolNavService patrolNavService;

	/**
	 * 列表
	 */
	@ApiOperation(value = "列表", response = VPage.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "标题", paramType = "query", required = false, dataType = "String"), })
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:list')")
	public R<VPage<PatrolNavVO>> list(@RequestParam Map<String, Object> params) {
		QueryWrapper<PatrolNavEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				PatrolNavEntity.class);
		queryWrapper.orderByDesc("id");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		IPage<PatrolNavEntity> pages = patrolNavService.page(VQueryKit.getPage(params), queryWrapper);
		return R.data(PatrolNavWrapper.build().toVPageVO(pages));
	}

	@GetMapping("/listStat")
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:list')")
	public R<Map<String, Object>> listStat(@RequestParam Map<String, Object> params) throws ParseException {
		QueryWrapper<PatrolNavEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				PatrolNavEntity.class);
		queryWrapper.select("count(id) as total,sum(id) as all");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		if (params.containsKey("createDate")) {
			queryWrapper.between("create_at",
					DateUtils.getStartOfDate(DateUtils.str_ToDate(params.get("createDate").toString())),
					DateUtils.getEndOfDate(DateUtils.str_ToDate(params.get("createDate").toString())));
		}
		Map<String, Object> stat = patrolNavService.getMap(queryWrapper);
		return R.data(stat);
	}

	@ApiOperation("导出")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:list')")
	@GetMapping("/export")
	public void export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
		QueryWrapper<PatrolNavEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				PatrolNavEntity.class);
		queryWrapper.orderByDesc("id");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		List<PatrolNavEntity> list = patrolNavService.list(queryWrapper);
		ExcelUtil<PatrolNavEntity> util = new ExcelUtil<>(PatrolNavEntity.class);
		util.exportExcel(response, list, "参数数据");
	}

	@ApiOperation("导出")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:list')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, PatrolNavEntity config) {
		List<PatrolNavEntity> list = patrolNavService.list(config);
		ExcelUtil<PatrolNavEntity> util = new ExcelUtil<>(PatrolNavEntity.class);
		util.exportExcel(response, list, "参数数据");
	}

	/**
	 * 信息
	 */
	@ApiOperation(value = "信息", response = PatrolNavVO.class)
	@GetMapping("/detail/{id}")
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:info')")
	public R<PatrolNavVO> detail(@PathVariable("id") Long id) {
		PatrolNavEntity patrolNav = patrolNavService.getById(id);
		return R.data(PatrolNavWrapper.build().entityVO(patrolNav));
	}

	/**
	 * 保存&更新
	 */
	@ApiOperation(value = "保存&更新", response = Boolean.class)
	@PostMapping("/submit")
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:save')")
	public R<PatrolNavVO> submit(@Valid @RequestBody PatrolNavVO patrolNav) {
		if (patrolNav.getId() == null) {
			patrolNav.setCreateAt(new Date());
			patrolNav.setIsDelete(0);
			patrolNav.setUpdateAt(new Date());
		}
		if (patrolNavService.saveOrUpdate(patrolNav)) {
			return R.ok(patrolNav);
		}
		return R.error("数据错误");
	}

	/**
	 * 删除
	 */
	@ApiOperation(value = "删除", response = Boolean.class)
	@DeleteMapping("/delete")
	@PreAuthorize("@ss.hasPermi('patrol:patrolnav:delete')")
	public R<Boolean> delete(@RequestBody Long[] ids) {
		return R.ok(patrolNavService.removeByIds(Arrays.asList(ids)));
	}

}
